{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "070da5b4-1314-422c-b827-b7465a763586",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2.4 微积分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7cbf2730-567b-43ba-8400-28883f477ebd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "h=0.10000,numerical limit=2.30000\n",
      "h=0.01000,numerical limit=2.03000\n",
      "h=0.00100,numerical limit=2.00300\n",
      "h=0.00010,numerical limit=2.00030\n",
      "h=0.00001,numerical limit=2.00003\n"
     ]
    }
   ],
   "source": [
    "# 2.4.1 导数\n",
    "%matplotlib inline\n",
    "import numpy as np\n",
    "#from matploblib_inline import backend_inline\n",
    "import torch\n",
    "\n",
    "def f(x):\n",
    "    return 3 * x**2 - 4 *x\n",
    "\n",
    "def numerical_lim(f,x,h):\n",
    "    return (f(x +h) - f(x)) /h\n",
    "h = 0.1\n",
    "for i in range(5):\n",
    "    print(f'h={h:.5f},numerical limit={numerical_lim(f,1,h):.5f}')\n",
    "    h *= 0.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6a2b50fc-f037-4352-a3b1-4dc10fa91658",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'NoneType' object has no attribute 'plot'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[6], line 41\u001b[0m\n\u001b[0;32m     39\u001b[0m     set_axes(axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend)\n\u001b[0;32m     40\u001b[0m x \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39marange(\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m0.1\u001b[39m)\n\u001b[1;32m---> 41\u001b[0m \u001b[43mplot\u001b[49m\u001b[43m(\u001b[49m\u001b[43mx\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[43mf\u001b[49m\u001b[43m(\u001b[49m\u001b[43mx\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mx\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mx\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mf(x)\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mlegend\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mf(x)\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mTangent line (x=1)\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n",
      "Cell \u001b[1;32mIn[6], line 36\u001b[0m, in \u001b[0;36mplot\u001b[1;34m(X, Y, xlabel, ylabel, legend, xlim, ylim, xscale, yscale, fmts, figsize, axes)\u001b[0m\n\u001b[0;32m     34\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m x, y, fmt \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mzip\u001b[39m(X, Y, fmts):\n\u001b[0;32m     35\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(x):\n\u001b[1;32m---> 36\u001b[0m         \u001b[43maxes\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mplot\u001b[49m(x, y, fmt)\n\u001b[0;32m     37\u001b[0m     \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m     38\u001b[0m         axes\u001b[38;5;241m.\u001b[39mplot(y, fmt)\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'plot'"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6276b9b8-62f0-4be7-aa5f-e20a9ec2ba63",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
